<template>
  <div class="login_container">
    <div class="box_container">
      <!-- 头像部分 -->
      <div class="avater">
        <img src="../assets/images/avater.jpg" alt="">
      </div>
      <!-- 登录表单 -->
      <el-form class="login_form" :model="loginFormData" :rules="loginFormRules" ref="loginForm">
        <!-- 用户名-->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="loginFormData.username"></el-input>
          <!-- <el-input prefix-icon="iconfont icon-user"></el-input> -->
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" type=password v-model="loginFormData.password"></el-input>
        </el-form-item>
        <!-- 登录和重置按钮 -->
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据对象
      loginFormData: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10个字符', trigger: 'blur' }
        ],
      }

    }

  },
  methods: {
    reset() {
      this.$refs.loginForm.resetFields();
    },
    login() {
      this.$refs.loginForm.validate(async (valid) => {
        // console.log(valid);
        if (valid) {
          // 利用.then解决Promise异步问题
          // this.$http.post('login', this.loginFormData).then((res) => {
          //   // console.log(res.data);
          //   if (res.data.meta.status !== 200) return console.log("登陆失败");
          //   console.log("登陆成功");
          // })
          // 利用acync await解决Promise异步问题
          const { data: res } = await this.$http.post('login', this.loginFormData);
          if (res.meta.status !== 200) return this.$msg.error("登陆失败");
          this.$msg.success("登陆成功");
          // 登陆成功后的行为：1.保存token 2.跳转页面
          window.sessionStorage.setItem('token', res.data.token);
          this.$router.push('/home')

        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;

  .box_container {
    width: 450px;
    height: 300px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;

    .avater {
      width: 130px;
      height: 130px;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 3px solid plum;
      border-radius: 50%;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 30px;
      // 防止表单输入框溢出
      box-sizing: border-box;
    }
  }
}
</style>